import core.ui.Edit;
import core.ui.Static;
import core.ui.Password;

import xcms.gui.Link;

/**
 * Class responsible for building CMS-styled user interface.
 */
class xcms.gui.CommonBuilder
{
    /**
     * Tab index counter.
     */
    this.tabIndex = 1;

    /**
     * @param subject (Window) Control to adjust.
     * @param x, y (int) Pixel coordinates.
     */
    this.adjustPosition = function( subject, x, y )
    {
        subject.setStyle( (x >= 0 ) ? 'left' : 'right', Math.abs( x ) + 'px' );
        subject.setStyle( (y >= 0 ) ? 'top' : 'bottom', Math.abs( y ) + 'px' );
    }

    /**
     * @param parent (Window) Parent window.
     * @param text (string) Text content.
     * @param x, y (int) Text coordinates in pixels.
     * @return Static
     */
    this.createText = function( parent, text, x, y )
    {
        var static = new core.ui.Static( parent );
        static.setStyle( 'position', 'absolute' );
        static.setStyle( 'line-height', '20px' );
        static.setStyle( 'height', '20px' );
        static.setStyle( 'overflow-x', 'visible' );
        static.setStyle( 'font-family', 'Arial' );
        static.setStyle( 'font-size', '16px' );
        static.setText( text );
        this.adjustPosition( static, x, y );
        return static;
    }

    /**
     * @param parent (Window) The parent window.
     * @param text (string) The link's text.
     * @param listener (object) The action listener.
     * @param x, y (int) The new text pixel coordinates.
     * @return Link
     */
    this.createLink = function( parent, text, listener, x, y )
    {
        var link = new Link( parent );
        link.setText( text );
        link.setTabIndex( this.tabIndex++ );
        link.addActionListener( listener );
        this.adjustPosition( link, x, y );
        return link;
    }

    /**
     *
     * @param parent (Window) The parent window.
     * @param x, y (int) The pixel coordinates.
     * @return Edit
     */
    this.createEdit = function( parent, x, y )
    {
        var edit = new core.ui.Edit( parent );
        edit.setStyle( 'position', 'absolute' );
        edit.setStyle( 'border', '1px solid #333333' );
        edit.setTabIndex( this.tabIndex++ );
        this.adjustPosition( edit, x, y );
        return edit;
    }

    /**
     *
     * @param parent (Window) The parent window.
     * @param x, y (int) The pixel coordinates.
     * @return Password
     */
    this.createPassword = function( parent, x, y )
    {
        var password = new core.ui.Password( parent );
        password.setStyle( 'position', 'absolute' );
        password.setStyle( 'border', '1px solid #333333' );
        password.setTabIndex( this.tabIndex++ );
        this.adjustPosition( password, x, y );
        return password;
    }
}